
void read_mode(void){
  pinMode(Data0, INPUT);
  pinMode(Data1, INPUT);
  pinMode(Data2, INPUT);
  pinMode(Data3, INPUT);
  pinMode(Data4, INPUT);
  pinMode(Data5, INPUT);
  pinMode(Data6, INPUT);
  pinMode(Data7, INPUT);

//  pinMode(Data0, INPUT_PULLUP);
//  pinMode(Data1, INPUT_PULLUP);
//  pinMode(Data2, INPUT_PULLUP);
//  pinMode(Data3, INPUT_PULLUP);
//  pinMode(Data4, INPUT_PULLUP);
//  pinMode(Data5, INPUT_PULLUP);
//  pinMode(Data6, INPUT_PULLUP);
//  pinMode(Data7, INPUT_PULLUP);
  
}

void write_mode(void){
  pinMode(Data0, OUTPUT);
  pinMode(Data1, OUTPUT);
  pinMode(Data2, OUTPUT);
  pinMode(Data3, OUTPUT);
  pinMode(Data4, OUTPUT);
  pinMode(Data5, OUTPUT);
  pinMode(Data6, OUTPUT);
  pinMode(Data7, OUTPUT);
}



// 读取数据
uint8_t read_data_bus(void) {
  return ((digitalRead(Data7) << 7) +
          (digitalRead(Data6) << 6) +
          (digitalRead(Data5) << 5) +
          (digitalRead(Data4) << 4) +
          (digitalRead(Data3) << 3) +
          (digitalRead(Data2) << 2) +
          (digitalRead(Data1) << 1) +
          digitalRead(Data0));
}

void write_data_bus(byte dataa){
  digitalWrite(Data0, dataa & 1);//也可使用移位 data & (1 << 0)
  digitalWrite(Data1, dataa & 2);
  digitalWrite(Data2, dataa & 4);
  digitalWrite(Data3, dataa & 8);
  digitalWrite(Data4, dataa & 16);
  digitalWrite(Data5, dataa & 32);
  digitalWrite(Data6, dataa & 64);
  digitalWrite(Data7, dataa & 128);
}



void readCommand() {
  for(uint8_t i=0; i< CMD_LEN; i++) cmdBuffer[i] = 0;  // clear command buffer
  char c; uint8_t idx = 0;                        // initialize variables
  do {
    if(Serial.available()) {
      c = Serial.read();
      cmdBuffer[idx++] = c;
    }
  } while (c != '\n' && idx < CMD_LEN);
  cmdBuffer[idx - 1] = 0;                     // change last newline to '\0' termination
}

// 返回字芯片字符串
uint32_t hexChip(char* dataa) {
  uint32_t re = 0;
//  if (dataa[0] >= 0x30 && dataa[0] <= 0x39)re += (dataa[0] - 0x30) * 100000;
  if (dataa[1] >= 0x30 && dataa[1] <= 0x39)re += (dataa[1] - 0x30) * 10000;
  if (dataa[2] >= 0x30 && dataa[2] <= 0x39)re += (dataa[2] - 0x30) * 1000;
  if (dataa[3] >= 0x30 && dataa[3] <= 0x39)re += (dataa[3] - 0x30) * 100;
  if (dataa[4] >= 0x30 && dataa[4] <= 0x39)re += (dataa[4] - 0x30) * 10;
  if (dataa[5] >= 0x30 && dataa[5] <= 0x39)re += (dataa[5] - 0x30) * 1;
  return re;
}

// converts character representing a hex nibble into 4-bit value
uint8_t hexDigit(char c) {
  if      (c >= '0' && c <= '9') return c - '0';
  else if (c >= 'a' && c <= 'f') return c - 'a' + 10;
  else if (c >= 'A' && c <= 'F') return c - 'A' + 10;
  else return 0;
}


// converts string containing a hex byte into 8-bit value
byte hexByte(char* a) {
  return ((hexDigit(a[0])*16) + hexDigit(a[1]));
}


// converts string containing a hex word into 16-bit value
uint16_t hexWord(char* dataa) {
  return ((hexDigit(dataa[0])*4096)+
    (hexDigit(dataa[1])*256)+
    (hexDigit(dataa[2])*16)+
    (hexDigit(dataa[3])));
}


float get_voltage (void) {
  float v_ADC = (analogRead(P_A6) / 1024.) * 5.;
  return (v_ADC / 2000.0 * (10000.0 + 2000.0));
}
